package com.xique.park.command.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.api.IParkIpcCommandApi;
import com.xique.park.bean.domain.ParkCarMngtype;
import com.xique.park.command.handler.base.BaseCommand;
import com.xique.park.constant.CommandConstant;
import com.xique.park.constant.ParkApiName;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 删除车辆管理类型
 *
 * @author WLD
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class DelMngTypeCommandHandler extends BaseCommand {

    private final IParkIpcCommandApi parkIpcCommandApi;

    @Override
    public String getServiceName() {
        return ParkApiName.COMMAND_MNG_TYPE_DEL;
    }

    @Override
    public String getVersion() {
        return ParkApiName.VER_1_0;
    }

    @Override
    public AjaxResult issue(Long parkId, Long laneId, String mngTypeIds, String sn,
                            MultipartFile file, String fileName, String cmd) {
        checkParkId(parkId);
        checkSn(sn);
        checkMngTypeIds(mngTypeIds);

        JSONObject baseObj = getBaseObj(sn, ParkApiName.COMMAND_MNG_TYPE_DEL, ParkApiName.VER_1_0);
        String[] checkMngTypeIdArray = mngTypeIds.split(",");
        List<ParkCarMngtype> parkCarMngTypeList = new ArrayList<>(checkMngTypeIdArray.length);
        ParkCarMngtype mngType = new ParkCarMngtype();
        for (String mngTypeId : checkMngTypeIdArray) {
            mngType.setId(Long.valueOf(mngTypeId));
            parkCarMngTypeList.add(mngType);
        }
        baseObj.put(CommandConstant.DATA, JSON.toJSON(parkCarMngTypeList));

        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSON.toJavaObject(baseObj, Map.class);

        AjaxResult issueResult = parkIpcCommandApi.issue(params);

        log.info("[停车系统]删除车辆管理类型命令下发，服务名：{}，版本号：{}，车场ID：{}，管理类型ID：{}，设备编号：{}，下发参数：{}，下发结果：{}",
                getServiceName(), getVersion(), parkId, mngTypeIds, sn, params, issueResult);

        updateCommonInfo(cmd, params, issueResult);

        return issueResult;
    }
}
